package cn.biplam.front.component.view.systemManage.sysprivi.user.manage.role;

import cn.biplam.common.ajxcode.SystemCode.RoleSelectNaviCode;
import cn.biplam.common.ajxcode.SystemCode.UserManageCode;
import cn.biplam.common.bean.systemManage.privi.UserManageBean;
import cn.biplam.common.opcode.SysManageCode.UserRoleOptCode;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.ComponentBase;
import cn.biplam.front.component.view.systemManage.sysprivi.role.manage.navi.RoleSelect;
import cn.biplam.front.component.view.systemManage.sysprivi.user.UserManage;
import cn.biplam.front.component.view.systemManage.sysprivi.user.manage.list.UserBase;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.template.control.PromptContent;

/**
 *  添加用户角色
 * @author XiaoRuxing Dec 14, 200910:49:50 AM
 * @version 1.0
 */
public class AddUserRole extends ComponentBase{

	private static final String cId = UserManageCode.User_Role_Add+".add";
	
	public AddUserRole(UserInfo user, String homeUrl, String recordId,
			KvMap<String, String> params) {
		super(user, homeUrl, recordId, params);
	}

	/**
	 * @return 获取权限选择界面
	 * @throws ErrorMessage 
	 */
	public HtmlWindow roleSelect() throws ErrorMessage{
		//获取角色导航树
		HtmlWindow tree = getRoleTree();
		//设置弹出窗口确定提交命令
		String script = AjxScript.getSpNoRefresh(SysName.PromoptContentID,
				UserManageCode.User_Role_Add_Submit,
				recordId);
		
		PromptContent prompt = new PromptContent(tree);
		prompt.setSubmitScript(script);
		HtmlWindow win = prompt.get();
		return win;
	}
	
	/**
	 * @return 添加角色权限提交
	 * @throws ErrorMessage 
	 */
	public HtmlWindow submit() throws ErrorMessage{
		//获取需要添加的用户角色列表
		String[] roleList = TreeParam.getTreeSelectList(params, 
				cId, 
				RoleSelectNaviCode.Navi);
		String userId = recordId;
		UserManageBean userManage = UserManageBean.getNewInstance(mUser.getUserID());
		if(roleList!=null){
			userManage.addUserRoles(userId, roleList, false);
		}
		//刷新用户角色信息列表
		HtmlWindow win = new HtmlDiv();
		win.bind(AjxScript.getA(UserBase.conId, UserManage.User_Base_Info));
		return win;
	}
	
	/**
	 * @return 权限导航树
	 */
	private HtmlWindow getRoleTree() throws ErrorMessage{
		RoleSelect role = new RoleSelect();
		//设置节点过滤条件为当前用户具有的角色不显示
		role.setFilterOperator(UserRoleOptCode.Query_User_Role_Exist,recordId);
		HtmlWindow roleTree =  role.get(mUser, sHomeUrl, cId);
		roleTree.setStyleHeight(200);//设置树高度
		roleTree.setStyleWidth(240);
		return roleTree;
	}
}
